package net.erl.objects.pattern;

import net.erl.objects.ErlTerm;
import net.erl.objects.ET;
import net.erl.objects.pattern.impl.*;

/**
 * Equivalent to net.util.jenco.erl.ET, but for constructing patterns.
 * All functions are prepended with 'p' to ease static importing.
 */
public class EP {
    private static final ErlAnyPattern ERL_ANY_PATTERN = new ErlAnyPattern();
    private static final ErlIsAtomPattern ERL_IS_ATOM_PATTERN = new ErlIsAtomPattern();
    private static final ErlIsListPattern ERL_IS_LIST_PATTERN = new ErlIsListPattern();
    private static final ErlIsNumberPattern ERL_IS_NUMBER_PATTERN = new ErlIsNumberPattern();
    private static final ErlIsTuplePattern ERL_IS_TUPLE_PATTERN = new ErlIsTuplePattern();
    private static final ErlListPattern ERL_LIST_PATTERN = new ErlListPattern();

    public static ErlPattern pterm(ErlTerm term) {
        return new ErlPatternImpl(term);
    }

    public static ErlPattern pvar(String name) {
        return new ErlVarPattern(name);
    }

    public static ErlPattern pvar(String name, ErlPattern andMatch) {
        return new ErlVarPattern(name, andMatch);
    }

    public static ErlPattern pany() {
        return ERL_ANY_PATTERN;
    }

    public static ErlPattern pis_atom() {
        return ERL_IS_ATOM_PATTERN;
    }

    public static ErlPattern pis_list() {
        return ERL_IS_LIST_PATTERN;
    }

    public static ErlPattern pis_number() {
        return ERL_IS_NUMBER_PATTERN;
    }

    public static ErlPattern pis_tuple() {
        return ERL_IS_TUPLE_PATTERN;
    }

    public static ErlPattern plist() {
        return ERL_LIST_PATTERN;
    }

    public static ErlPattern plist(EPMatchable ... eps) {
        return new ErlListPattern(eps);
    }

    public static ErlPattern ptuple(EPMatchable ... eps) {
        return new ErlTuplePattern(eps);
    }

    /* Specific types - convenience. */

    public static ErlPattern patom(String v) {
        return pterm(ET.atom(v));
    }

    public static ErlPattern pbinary() {
        return pterm(ET.binary());
    }

    public static ErlPattern pbinary(byte ... bytes) {
        return pterm(ET.binary(bytes));
    }

    public static ErlPattern pbinary(String utf8) {
        return pterm(ET.binary(utf8));
    }

    public static ErlPattern pnumber(Number n) {
        return pterm(ET.number(n));
    }

    public static ErlPattern pnumber(int n) {
        return pterm(ET.number(n));
    }

    public static ErlPattern pnumber(long n) {
        return pterm(ET.number(n));
    }

    public static ErlPattern pnumber(float n) {
        return pterm(ET.number(n));
    }

    public static ErlPattern pnumber(double n) {
        return pterm(ET.number(n));
    }

    public static ErlPattern plist(String utf8) {
        return pterm(ET.list(utf8));
    }

    public static ErlPattern plist(String utf8, boolean toBytes) {
        return pterm(ET.list(utf8, toBytes));
    }

}